﻿Imports System.Data.OleDb
Public Class ViewMembersInEvent
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Params("Event_FK") = "" Then
            Response.Redirect("Events.aspx")
        End If
        'Check if user is the creator of the event.
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlStringCheckUser As String = "SELECT * FROM Events WHERE EventCreator = @f1 AND ID = @f2"
        Dim cmdCheck As OleDbCommand = New OleDbCommand(SqlStringCheckUser, oleDbConn)
        cmdCheck.CommandType = CommandType.Text
        cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmdCheck.Parameters.AddWithValue("@f2", Request.Params("Event_FK"))

        'Getting records (only 1 line for selected tourist)
        Dim dr As OleDbDataReader = cmdCheck.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("ID")) Then
                btn_DelEvents.Visible = True
                btn_DelEvents.Enabled = True
            End If
        End If

        If IsPostBack = False Then
            'Count activity
            Dim SqlString As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Events")
            cmd.ExecuteNonQuery()
        End If
        oleDbConn.Close()
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("FirstName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("Tourist", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM Event_Attendees_WithDetails WHERE Event_FK = @f1 AND " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.SelectParameters.Add("@f1", Request.Params("Event_FK"))
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Sub SqlDataSource1_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        If (e.AffectedRows < 13) Then
            DataPagerProfiles.Visible = False
        Else
            DataPagerProfiles.Visible = True
        End If
    End Sub

    Protected Sub ListView2_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerProfiles.Visible = (DataPagerProfiles.PageSize < DataPagerProfiles.TotalRowCount)
    End Sub

    Protected Function getAttendance(ByVal attendees As Integer, ByVal capacity As Integer) As String
        If attendees = capacity Then
            Return "Event Full"
        Else
            Return "Attendance : " & attendees & "/" & capacity
        End If
    End Function
    Protected Function getEventTime(ByVal eventTime As DateTime) As String
        Dim stringDate As String = "Event Date : "
        stringDate += eventTime.ToString("dd-MM-yyyy")
        Return stringDate
    End Function

    Protected Sub btn_DelEvents_Click(sender As Object, e As EventArgs) Handles btn_DelEvents.Click
        'First remove people attending the event
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim SqlString As String = "DELETE FROM Event_Attendees WHERE Event_FK = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("Event_FK"))
        cmd.ExecuteNonQuery()

        'Deleting the event.
        Dim SqlStringEvent As String = "DELETE FROM Events WHERE ID = @f1"
        Dim cmdEvent As OleDbCommand = New OleDbCommand(SqlStringEvent, oleDbConn)
        cmdEvent.CommandType = CommandType.Text
        cmdEvent.Parameters.AddWithValue("@f1", Request.Params("Event_FK"))
        cmdEvent.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Events.aspx")
    End Sub
End Class